//路由配置信息

//引入一级路由组件
import Home from "../pages/Home";
import Search from "../pages/Search";
import Login from "../pages/Login";
import Register from "../pages/Register";
import Detail from "../pages/Detail";
import AddCartSuccess from "../pages/AddCartSuccess";
import ShopCart from "../pages/ShopCart";
import Trade from "../pages/Trade";
import Pay from "../pages/Pay";
import PaySuccess from "../pages/PaySuccess";
import Center from "../pages/Center";

//引入二级路由组件
import MyOrder from "../pages/Center/myOrder";
import GroupOrder from "../pages/Center/groupOrder";

/* 
component: () => import('@/pages/Search')
1. import(modulePath): 动态import引入模块, 被引入的模块会被单独打包
2. 组件配置的是一个函数, 函数中通过import动态加载模块并返回, 
    初始时函数不会执行, 第一次访问对应的路由才会执行, 也就是说只有一次请求对应的路由路径才会请求加载单独打包的js
作用: 用于提高首屏的加载速度
*/

export default [
  //重定向，在项目跑起来时，访问/，回到首页
  {
    path: "*",
    redirect: "/home",
    meta: { show: true },
  },
  {
    path: "/center",
    component: () => import('../pages/Center'),
    meta: { show: true }, //路由元信息，用于判断是否当前组件显示
    //二级路由组件
    children: [
      {
        path: "myorder",
        component: () => import('../pages/Center/myOrder'),
      },
      {
        path: "grouporder",
        component: () => import('../pages/Center/groupOrder'),
      },
      {
        path: "/center",
        redirect: "/center/myorder",
      },
    ],
  },
  {
    path: "/paysuccess",
    component: () => import('../pages/PaySuccess'),
    meta: { show: true }, //路由元信息，用于判断是否当前组件显示
    /* 只有从支付界面, 才能跳转到支付成功的界面 */
    beforeEnter(to, from, next) {
      if (from.path === "/pay") {
        next();
      } else {
        next("/pay");
      }
    },
  },
  {
    path: "/pay",
    component: () => import('../pages/Pay'),
    meta: { show: true }, //路由元信息，用于判断是否当前组件显示
    /* 只有从交易界面, 才能跳转到支付的界面 */
    beforeEnter: (to, from, next) => {
      if (from.path == "/trade") {
        next();
      } else {
        next(false);
      }
    },
  },
  {
    path: "/trade",
    component: () => import('../pages/Trade'),
    meta: { show: true }, //路由元信息，用于判断是否当前组件显示
    // 路由独享守卫(只负责这个路由)
    beforeEnter: (to, from, next) => {
      //去交易页面，必须从购物车页面过来
      if (from.path == "/shopcart") {
        next();
      } else {
        //由其他路由组件而来，停留在本页
        next(false);
      }
    },
  },
  {
    path: "/shopcart",
    component: () => import('../pages/ShopCart'),
    meta: { show: true }, //路由元信息，用于判断是否当前组件显示
  },
  {
    path: "/addCartSuccess",
    name: "addCartSuccess",
    component: () => import('../pages/AddCartSuccess'),
    meta: { show: true }, //路由元信息，用于判断是否当前组件显示
  },
  {
    path: "/detail/:skuid",
    component: () => import('../pages/Detail'),
    meta: { show: true }, //路由元信息，用于判断是否当前组件显示
  },
  {
    path: "/home",
    component: () => import('../pages/Home'),
    meta: { show: true }, //路由元信息，用于判断是否当前组件显示
  },
  {
    path: "/search/:keyword?",
    //path: "/search",
    //路由懒加载（即按需引入）
    component: () => import('../pages/Search'),
    meta: { show: true },
    name: "search",
  },
  {
    path: "/register",
    component: () => import('../pages/Register'),
    meta: { show: false },
  },
  {
    path: "/login",
    component: () => import('../pages/Login'),
    meta: { show: false },
  },
];
